<?php

/**
 * \file
 * Classe pour l'authentification grâce à la base de données.
 */

namespace pilar\AuthenticationManager /* \cond */ ; /* \endcond */
{

/**
 * Authentification grâce à la base de données.
 *
 * \since 28/06/2009
 * \author Dirk Marski <dirk.marski@gmail.com>
 */
class DatabaseAuth 
extends AbstractAuthenticationObject 
implements AuthenticationObject {


public function authenticate($userId = null, $pwd = null) {
	if ($userId != null && $pwd != null) {
		$pwd = $this->encodePassword($pwd);

		$authCollection = $this->_authEntity->newCollection();
		$authCollection->where(\andy(
			\eq($authCollection->member($this->_loginMemberName),\cst($userId)),
			\eq($authCollection->member($this->_passwordMemberName), \cst($pwd))
		));

		foreach ($authCollection as $authenticated) {
			$this->_sessionManager->addVar('pilar_userId', $authenticated->id());
		}
	} else {
		$r = new \pilar\Redirections\Redirection(
	  array('redirection' => $this->_authenticationPageUrl)
	 );
	 $r->redirect();
	}
	
	return true;
}


public function checkAuthentication() {
	return true;
}


public function setSessionManager(
                 \pilar\SessionManager\SessionManager $sessionManager) {
	$this->_sessionManager = $sessionManager;
}


} // Class.
 
} // Namespace.
